package com.huha.tools.utils;

import java.io.*;

import static com.huha.tools.Constants.ERROR_TEXT_INVALID_INPUT;

/**
 * @Description 生成索引
 * @Author gyl
 * @Date 2023-02-02
 * @Version 1.0
 **/
public class CreateEpointBZBIndex
{
    public static void main(String[] args) throws IOException {
        // 创建流对象
        BufferedReader br = new BufferedReader(new FileReader("E:\\ProgramData\\Desktop\\Untitled-7.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
        // 定义字符串,保存读取的一行文字
        String line = null;
        // 循环读取,读取到最后返回null
        int i = 0;
        while ((line = br.readLine()) != null) {
            if (line.startsWith("表")) {
                i++;
                System.out.println(line);
                System.out.println("------");
                String[] split = line.split(":");
                String tableName = split[1].split(",")[0];
                String indexName = split[2].split(",")[0];
                String indexs = split[3].substring(0, split[3].indexOf('索'));
                bw.write("-------------- NO." + i + "  " + indexName + " --------------");
                bw.newLine();
                // sqlserver
                // String sql = "if exists(select * from sysindexes where id=object_id('" +
                // tableName + "') and name='"
                // + indexName + "')\n" + " drop index " + indexName + " on " + tableName +
                // ";\n"
                // + "create index " + indexName + " on " + tableName + " (" + indexs + ");\n";
                // mysql
                String sql = "if exists (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = '"
                        + tableName + "' AND index_name = '" + indexName + "')" + " drop index " + indexName + " on "
                        + tableName + ";\n" + "create index " + indexName + " on " + tableName + " (" + indexs + ");\n";
                System.out.println(sql);
                bw.write(sql);
                bw.newLine();
            }
            System.out.println(i);
        }
        // 释放资源
        br.close();
        bw.close();
    }

    public static String createIndex(String line, String sqltype){
        if (line.startsWith("表")) {
            System.out.println(line);
            System.out.println("------");
            String[] split = line.split(":");
            String tableName = split[1].split(",")[0];
            String indexName = split[2].split(",")[0];
            String indexs = split[3].substring(0, split[3].indexOf('索'));

            String sql = "";
            switch (sqltype.toLowerCase()){
                case "mysql":
                    sql = "if exists (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = '"
                            + tableName + "' AND index_name = '" + indexName + "')" + " drop index " + indexName + " on "
                            + tableName + ";\n" + "create index " + indexName + " on " + tableName + " (" + indexs + ");\n";
                    break;
                case "sqlserver":
                    sql = "if exists(select * from sysindexes where id=object_id('" +
                            tableName + "') and name='"
                            + indexName + "')\n" + " drop index " + indexName + " on " + tableName +
                            ";\n"
                            + "create index " + indexName + " on " + tableName + " (" + indexs + ");\n";
                    break;
                default:
                    break;
            }

            System.out.println(sql);
            return sql;
        }
        return ERROR_TEXT_INVALID_INPUT;
    }
}
